home *** CD-ROM | disk | FTP | other *** search
- Path: rover.ucs.ualberta.ca!news
- From: ryangall@gpu.srv.ualberta.ca (Ryan Gallagher)
- Newsgroups: comp.lang.c++,de.comp.lang.c++,ualberta.cmput.201
- Subject: Another function pointer from a class question?!
- Date: 31 Mar 1996 11:59:21 GMT
- Organization: University of Alberta, Edmonton, Canada
- Message-ID: <4jls2p$bac@pulp.ucs.ualberta.ca>
- NNTP-Posting-Host: async6-4.remote.ualberta.ca
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.99.2
-
-
- I have an error that I am unsure of.....I want to have a derived class
- that I can pass a function pointer that I will use in a print function.
- If I dont use the print function, and just have the class print off the
- elements in the main function, then it works fine....the class itself
- works
- well....its just that I get a linker error when I try and run it when
- using
- the print function. Since I want to be able to use any structure for
- the list, I will have to supply some sort of printing function to the
- class,
- otherwise, it will not nessesarily know what to do when printing each
- member.
- Anyways, this is my first attempt at a template, and derived class....so
- bear with me if I've made any really stupid mistakes.
-
-
- in list.h....I have the pure virtual class
-
- template<class T>
- class list{
-
- public:
-
- virtual T Add(T)=0;
- virtual T Remove(T)=0;
- virtual T POPfirst()=0;
- virtual T POPlast()=0;
- virtual int Inlist(T)=0;
- virtual int isempty()=0;
- virtual void print(void (*)(T))=0;
-
- };
-
-
- ...................................
-
- //this file is dl_list.cpp
-
- #include "list.h"
-
- template<class T>
- // I used to have this next line, as the delaration but it didnt work,
- //class dl_list : public list<T>{
- // it kept saying that I couldnt use a abstract class directly.
-
- class dl_list{
- struct node{
- T data;
- node *next;
- node *prev;
- node(T d,node *n,node *p) { data = d; next = n; prev =p;}
- };
-
- node *S;
- node *F;
- node *C;
-
- public:
-
- dl_list();
- ~dl_list();
-
- int Add(T);
- T Remove();
- T POPfirst();
- T POPlast();
- int empty() { return (F==S); }
- void print(void (*)(T));
-
- };
-
- /***********************************************************************
- *
- * print: prints off the data in the list.
- *
- ************************************************************************/
-
- template<class T> void list<T>::print(void (*pfunc)(T))
- {
- node *X=S;
-
- cout << endl << endl <<endl;
- cout << "THE LIST" << endl << "--------" << endl;
-
-
- if(X!=F)
-
- do
- {
- X=X->next;
- (*pfunc)(X->data);
- }
- while( X != F && X);
-
- return 0;
-
- }
-
- //*************************************************************
-
- void printint(int d)
- {
- printf("( %i )\n",d);
- }
-
- //*************************************************************
-
- int main(void)
- {
- int R[10]={1,2,3,4,5,6,7,8,9,0};
- int n;
-
- dl_list<int> M;
- clrscr();
-
- for(n=0; n<10; n++)
- {
- M.Add(R[n]);
- }
-
- M.print(printint);
-
-
-
- }
- *****************LINKER ERROR IS ****************
-
- undefined symbol dl_list<int>::print( void (near*)(int)) in module
- DL_LIST.CPP
-
-
-
-
- I hope you can help me out...........
- thanks alot, Mail me back if possible!
- ------------------------------------------
-
-
- Ryan G
-
-